<% contract_creation_code = contract_creation_code(@address) %>

<section class="container">
  <%= render BlockScoutWeb.AddressView, "overview.html", assigns %>

  <div class="card">
    <%= render BlockScoutWeb.AddressView, "_tabs.html", assigns %>
    <div class="card-body">
      <%= if match?({:selfdestructed, _}, contract_creation_code) do %>
        <div class="button button-disabled button-sm float-right ml-3">
          <%= gettext("Verify & Publish") %>
        </div>
      <% else %>
        <%= if !BlockScoutWeb.AddressView.smart_contract_verified?(@address) do %>
          <%= link(
                gettext("Verify & Publish"),
                to: address_verify_contract_path(@conn, :new, @address.hash),
                class: "button button-primary button-sm float-right ml-3",
                "data-test": "verify_and_publish"
              ) %>
        <% end %>
      <% end %>
      <%= if BlockScoutWeb.AddressView.smart_contract_verified?(@address) do %>
        <div class="mb-4">
          <dl class="row">
            <dt class="col-md-2 text-muted"><%= gettext "Contract name:" %></dt>
            <dd class="col-md-4"><%= @address.smart_contract.name %></dd>
            <div class="d-none d-sm-block d-md-none"></br></br></div>
            <div class="d-block d-sm-none"></br></br></div>
            <dt class="col-md-2 text-muted"><%= gettext "Optimization enabled" %></dt>
            <dd class="col-md-4"><%= format_optimization_text(@address.smart_contract.optimization) %></dd>
          </dl>
          <dl class="row">
            <dt class="col-md-2 text-muted"><%= gettext "Compiler version" %></dt>
            <dd class="col-md-4"><%= @address.smart_contract.compiler_version %></dd>
            <div class="d-none d-sm-block d-md-none"></br></br></div>
            <div class="d-block d-sm-none"></br></br></div>
            <%= if @address.smart_contract.optimization && @address.smart_contract.optimization_runs do %>
              <dt class="col-md-2 text-muted"><%= gettext "Optimization runs" %></dt>
              <dd class="col-md-4"><%= @address.smart_contract.optimization_runs %></dd>
            <% end %>
          </dl>
          <%= if @address.smart_contract.evm_version do %>
            <dl class="row">
              <dt class="col-sm-4 col-md-2 text-muted"><%= gettext "EVM Version" %></dt>
              <dd class="col-sm-8 col-md-10"><%= @address.smart_contract.evm_version %></dd>
            </dl>
          <% end %>
        <hr/>
        <%= if @address.smart_contract.constructor_arguments do %>
          <section>
            <div class="d-flex justify-content-between align-items-baseline">
              <h3><%= gettext "Constructor Arguments" %></h3>
            </div>
            <div class="tile tile-muted mb-4">
              <pre class="pre-wrap pre-scrollable"><code class="nohighlight"><%= raw(format_constructor_arguments(@address.smart_contract)) %></code>
              </pre>
            </div>
          </section>
        <% end %>
        <section>
          <div class="d-flex justify-content-between align-items-baseline">
            <h3><%= gettext "Contract source code" %></h3>
            <button type="button" class="button button-secondary button-sm" id="button" data-toggle="tooltip" data-placement="top" data-clipboard-text="<%= @address.smart_contract.contract_source_code %>" aria-label="Copy Contract Source Code">
              <%= gettext "Copy Source Code" %>
            </button>
          </div>
          <div class="tile tile-muted mb-4">
            <pre class="pre-scrollable line-numbers" data-activate-highlight><code class="solidity"><%= for {line, number} <- contract_lines_with_index(@address.smart_contract.contract_source_code, @address.smart_contract.inserted_at) do %><div data-line-number="<%= number %>"><%= line %></div><% end %></code></pre>
          </div>
        </section>

        <section>
          <div class="d-flex justify-content-between align-items-baseline">
            <h3><%= gettext "Contract ABI" %></h3>
            <button type="button" class="btn-line" id="button" data-clipboard-text="<%= format_smart_contract_abi(@address.smart_contract.abi) %>" aria-label="Copy Contract ABI">
              <%= gettext "Copy ABI" %>
            </button>
          </div>
          <div class="tile tile-muted mb-4">
            <pre class="pre-wrap pre-scrollable"><code class="nohighlight"><%= format_smart_contract_abi(@address.smart_contract.abi) %></code>
            </pre>
          </div>
        </section>

      <% end %>
      <section>
        <%= case contract_creation_code do %>
          <% {:selfdestructed, transaction_init} -> %>
            <div class="d-flex justify-content-between align-items-baseline">
              <h3><%= gettext "Contract Creation Code" %></h3>
              <button type="button" class="btn-line" id="button" data-clipboard-text="<%= transaction_init %>" aria-label="copy contract creation code">
                <%= gettext "Copy Contract Creation Code" %>
              </button>
            </div>
            <div class="alert alert-info">
              <p><%= gettext "Contracts that self destruct in their constructors have no contract code published and cannot be verified." %></p>
              <p><%= gettext "Displaying the init data provided of the creating transaction." %></p>
            </div>
            <div class="tile tile-muted">
              <pre class="pre-wrap pre-scrollable"><code class="nohighlight"><%= transaction_init %></code></pre>
            </div>
          <% {:ok, contract_code} -> %>
            <div class="d-flex justify-content-between align-items-baseline">
              <h3><%= gettext "Contract Byte Code" %></h3>
              <button type="button" class="btn-line" id="button" data-clipboard-text="<%= contract_code %>" aria-label="copy contract creation code">
                <%= gettext "Copy Contract Byte Code" %>
              </button>
            </div>
            <div class="tile tile-muted">
              <pre class="pre-wrap pre-scrollable"><code class="nohighlight"><%= contract_code %></code></pre>
            </div>
        <% end %>
      </section>

      <%= if BlockScoutWeb.AddressView.smart_contract_verified?(@address) do %>
        <%= if @address.smart_contract.external_libraries && @address.smart_contract.external_libraries != [] do %>
          <section>
            <div class="d-flex justify-content-between align-items-baseline">
              <h3><%= gettext "External libraries" %></h3>
            </div>
            <div class="tile tile-muted mb-4">
              <pre class="pre-wrap pre-scrollable"><code class="nohighlight"><%= raw(format_external_libraries(@address.smart_contract.external_libraries)) %></code>
              </pre>
            </div>
          </section>
        <% end %>
      <% end %>
    </div>
  </div>
</section>
